#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
简化版经销商查询接口调用示例

此脚本展示如何调用新增的 /api/distributor/simple 接口，
该接口只返回指定字段：distributorName、lat、lng、id、ico、nation
"""

import requests
import json

def query_distributor_simple(token, page=1, page_size=10):
    """
    调用简化版经销商查询接口
    
    Args:
        token (str): 访问令牌
        page (int): 页码，默认为1
        page_size (int): 每页数量，默认为10
        
    Returns:
        dict: 查询结果
    """
    # API接口地址
    url = "http://localhost:8091/api/distributor/simple"
    
    # 请求头 - 使用Bearer格式
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {token}"
    }
    
    # 请求体
    data = {
        "page": page,
        "page_size": page_size
    }
    
    try:
        # 发送POST请求
        response = requests.post(url, headers=headers, data=json.dumps(data))
        
        # 检查响应状态
        response.raise_for_status()
        
        # 返回解析后的JSON数据
        return response.json()
        
    except requests.exceptions.RequestException as e:
        print(f"请求异常: {e}")
        return None
    except json.JSONDecodeError as e:
        print(f"JSON解析错误: {e}")
        return None

if __name__ == "__main__":
    # 示例使用
    # 请替换为实际有效的token
    TOKEN = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjczQURFQjU"
    
    # 调用接口
    print("正在调用简化版经销商查询接口...")
    result = query_distributor_simple(TOKEN, page=1, page_size=10)
    
    if result:
        print("\n查询成功！")
        print(f"状态码: {result.get('code')}")
        print(f"消息: {result.get('msg')}")
        print(f"总数: {result.get('data', {}).get('total')}")
        print(f"页码: {result.get('data', {}).get('page')}")
        print(f"每页数量: {result.get('data', {}).get('page_size')}")
        
        # 打印按国家分组的经销商信息
        print("\n经销商信息（按国家分组）:")
        distributor_list = result.get('data', {}).get('list', {})
        
        for nation, distributors in distributor_list.items():
            print(f"\n国家: {nation}")
            print(f"经销商数量: {len(distributors)}")
            
            for i, distributor in enumerate(distributors, 1):
                print(f"\n  经销商 {i}:")
                print(f"    ID: {distributor.get('id')}")
                print(f"    名称: {distributor.get('distributorName')}")
                print(f"    图标: {distributor.get('ico')}")
                print(f"    纬度: {distributor.get('lat')}")
                print(f"    经度: {distributor.get('lng')}")
    else:
        print("查询失败，请检查服务是否正常运行以及token是否正确。")